package fun.hijklmn.magic.admin.server.controller;

import fun.hijklmn.magic.admin.common.pojo.User;
import fun.hijklmn.magic.admin.common.vo.req.UserLoginReqVO;
import fun.hijklmn.magic.admin.common.vo.req.UserReqVO;
import fun.hijklmn.magic.admin.common.vo.resp.DirPermissionRespVO;
import fun.hijklmn.magic.admin.common.vo.resp.UserRespVO;
import fun.hijklmn.magic.admin.server.handler.JwtHandler;
import fun.hijklmn.magic.admin.server.pojo.SysUser;
import fun.hijklmn.magic.admin.server.properties.AdminProperties;
import fun.hijklmn.magic.admin.server.properties.JwtProperties;
import fun.hijklmn.magic.admin.server.service.RolePermissionService;
import fun.hijklmn.magic.admin.server.service.UserService;
import fun.hijklmn.magic.common.enums.ResponseCodeEnum;
import fun.hijklmn.magic.common.utils.AesSecurityUtil;
import fun.hijklmn.magic.common.utils.ResultVOUtil;
import fun.hijklmn.magic.common.utils.RsaSecurityUtil;
import fun.hijklmn.magic.common.vo.resp.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description
 * @Author fs
 * @Date 2021/6/19 10:24
 * @Version 0.0.1-SNAPSHOT
 **/
@Api(value = "用户管理后台接口", tags = "用户服务前端控制器")
@RequestMapping("user")
@RestController
public class UserController {

    @Autowired
    private JwtHandler jwtHandler;

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private AdminProperties adminProperties;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @ApiOperation(value = "用户登录接口", tags = "用户服务前端控制器")
    @PostMapping("login")
    public ResultVO<List<DirPermissionRespVO>> login(@RequestBody UserLoginReqVO userLoginReqVO, HttpServletResponse response) {

        SysUser sysUser = (SysUser) userDetailsService.loadUserByUsername(userLoginReqVO.getUsername());

        String loginPassword = RsaSecurityUtil.decrypt(adminProperties.getRsaPrivateKey(), userLoginReqVO.getPassword());

        String originPassword = AesSecurityUtil.decrypt(adminProperties.getAesKey(), sysUser.getPassword());

        if (!loginPassword.equals(originPassword)) {
            return ResultVOUtil.parse(ResponseCodeEnum.ERROR, "密码错误");
        }

        String token = jwtHandler.createToken(sysUser.getUser().getId(), sysUser.getUser().getUserName(), sysUser.getRole().getCode(), false);

        response.setHeader(jwtProperties.getTokenHeader(), token);

        List<DirPermissionRespVO> dirPermissionRespVOList = rolePermissionService.queryPermissionCacheByRoleId(sysUser.getRole().getId());

        return ResultVOUtil.success(dirPermissionRespVOList);

    }

    @ApiOperation(value = "保存用户接口", tags = "用户服务前端控制器")
    @PostMapping("save")
    public ResultVO<UserRespVO> save(@RequestBody UserReqVO userReqVO) {
        User user = userService.save(userReqVO);
        UserRespVO userRespVO = new UserRespVO();
        BeanUtils.copyProperties(user, userRespVO);
        return ResultVOUtil.success(userRespVO);
    }

}
